<template>
  <el-dialog
    :title="importFeeObj.title"
    append-to-body
    :visible.sync="importFeeObj.show"
    :close-on-click-modal="false"
    width="900px"
  >
    <el-row>
      <upload-excel
        importText="导入缴费信息"
        :on-success="generateData"
      ></upload-excel>
      <el-button type="success" @click="downloadClick" size="mini"
        >下载模板</el-button
      >
    </el-row>
    <div class="mb-10" style="color:red;margin-left:40px;">
      注意：导入时请保证专利申请号存在，同一专利申请号的缴费类型不重复，日期格式（
      如：2020-01-01）单元格为文本格式，缴费状态请填写“未缴费”或“已缴费”！
    </div>
    <el-table :data="dataList" border style="width: 100%;" size="mini">
      <el-table-column align="center" width="50" type="index"></el-table-column>
      <el-table-column
        v-for="(item, index) in dataTableHeader"
        :prop="item.prop"
        align="center"
        :label="item.label"
        :width="item.width"
        :key="index"
        :show-overflow-tooltip="'' || item.tooltiop"
      ></el-table-column>
    </el-table>
    <div class="mb-10" style="color:red;margin-left:40px;">
      {{ this.message }}
    </div>
  </el-dialog>
</template>
<script>
import { placeholderTips } from "@u/validate";
import timeFormat from "@u/timeFormat";
import UploadExcel from "@/components/UploadExcel";
import { importPatentFeeApi } from "@s/api";
import moment from "moment";
export default {
  name: "dialogImportPatent",
  data() {
    return {
      placeholderTips: placeholderTips,
      dataList: [],
      message: "",
      dataTableHeader: [
        { prop: "applicationNumber", label: "专利申请号", width: "" },
        { prop: "statusShow", label: "缴费状态", width: "" },
        { prop: "feeType", label: "费用种类", width: "" },
        { prop: "feeAmountShow", label: "应缴金额", width: "" },
        { prop: "expiryDate", label: "缴费截止日", width: "" },
        { prop: "payTime", label: "缴费日期", width: "" },
        { prop: "payName", label: "缴费人姓名", width: "" },
        { prop: "billCode", label: "票据代码", width: "" },
        { prop: "billNumber", label: "票据号码", width: "" },
        { prop: "overdueAmountShow", label: "应交滞纳金额", width: "" }
      ],

      tabHeader: [
        "专利申请号",
        "缴费状态",
        "费用种类",
        "应缴金额",
        "缴费截止日",
        "缴费日期",
        "缴费人姓名",
        "票据代码",
        "票据号码",
        "应交滞纳金额"
      ]
    };
  },
  props: ["importFeeObj"],
  components: { UploadExcel },
  methods: {
    initDialog() {
      this.dataList = [];
      this.message = "";
    },
    generateData({ header, results }) {
      this.message = "";
      let json = {
        feeList: []
      };
      this.tabHeader = header;

      results.forEach(item => {
        // 接收参数
        var newItem = {
          applicationNumber: String(item[this.tabHeader[0]]),
          statusShow: String(item[this.tabHeader[1]]),
          status: "",
          feeType: item[this.tabHeader[2]],
          feeAmountShow: String(item[this.tabHeader[3]]),
          feeAmount: "",
          expiryDate: item[this.tabHeader[4]]
            ? moment(item[this.tabHeader[4]]).format("YYYY-MM-DD")
            : "",
          payTime: item[this.tabHeader[5]]
            ? moment(item[this.tabHeader[5]]).format("YYYY-MM-DD")
            : "",
          payName: item[this.tabHeader[6]] ? item[this.tabHeader[6]] : "",
          billCode: item[this.tabHeader[7]] ? item[this.tabHeader[7]] : "",
          billNumber: item[this.tabHeader[8]] ? item[this.tabHeader[8]] : "",
          overdueAmountShow: item[this.tabHeader[9]]
            ? item[this.tabHeader[9]]
            : "",
          overdueAmount: ""
        };
        json.feeList.push(newItem);
      });
      // 数据校验
      for (var i = 0; i < json.feeList.length; i++) {
        var item = json.feeList[i];
        if (
          !item.applicationNumber ||
          !item.statusShow ||
          !item.feeType ||
          !item.feeAmountShow
        ) {
          this.message =
            "导入失败：专利申请号、缴费状态、费用种类、应缴金额必须填写！";
          return;
        }
        if (!this.validatePatentNumber(item.applicationNumber)) {
          this.message = "导入失败：专利申请号请输入数字为主的编号！";
          return;
        }
        if (item.statusShow != "未缴费" && item.statusShow != "已缴费") {
          this.message = '导入失败：缴费状态请填写"未缴费"或者"已缴费"！';
          return;
        } else {
          item.status = item.statusShow == "已缴费" ? "3" : "0";
        }
        if (!this.validateNumber(item.feeAmountShow)) {
          this.message =
            "导入失败：应缴费金额请输入正数的数字或最多保留两位小数！";
          return;
        } else {
          if (item.feeAmountShow) {
            item.feeAmount = timeFormat.yuanToCent(item.feeAmountShow);
          }
        }
        if (!this.validateNumber(item.overdueAmountShow)) {
          this.message =
            "导入失败：应交滞纳金额请输入正数的数字或最多保留两位小数！";
          return;
        } else {
          if (item.overdueAmountShow) {
            item.overdueAmount = timeFormat.yuanToCent(item.overdueAmountShow);
          }
        }
      }
      json.feeList = JSON.parse(JSON.stringify(json.feeList));
      window.console.log(json);
      importPatentFeeApi(json).then(res => {
        if (!res.code && res.length == 0) {
          this.$message({
            type: "success",
            message: "导入成功！"
          });
          this.dataList = JSON.parse(JSON.stringify(json.feeList));
          this.$emit("reflash");
        } else if (!res.code && res.length > 0) {
          this.message = "导入失败：";
          for (var j = 0; j < res.length; j++) {
            this.message = this.message + res[j] + ";";
          }
        }
      });
    },
    /*限制  专利申请号 */
    validatePatentNumber(value) {
      // var reg = /^(\d+|\d+\.\d{1,2})$/;  // 含0
      let reg = /^[a-zA-Z0-9_.-]{4,50}$/;
      if (value && !reg.test(value)) {
        return false;
      } else {
        return true;
      }
    },
    /*限制  正数且最多保留两位小数 */
    validateNumber(value) {
      var reg = /^(\+?[1-9][0-9]*|\d+\.\d{1,2})$/;
      if (value && !reg.test(value)) {
        return false;
      } else {
        return true;
      }
    },
    downloadClick() {
      window.open(
        "https://patent-manage.oss-cn-shanghai.aliyuncs.com/template/%E7%BC%B4%E8%B4%B9%E4%BF%A1%E6%81%AF%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xls"
      );
    }
  },
  watch: {
    "importFeeObj.show"(val) {
      if (val) {
        this.initDialog();
      }
    }
  }
};
</script>
<style lang="scss" scoped></style>
